package sharefolder

import (
	"context"
	"errors"
	"gitlab.local/DO-module/new-filemanage-module/src/core/fileoperate"
	"gitlab.local/DO-module/new-filemanage-module/src/service/folder"
	"gitlab.local/DO-module/new-filemanage-module/src/service/user"
	"gitlab.local/TerraMaster/tos-modules/core/acl"
	"gitlab.local/TerraMaster/tos-modules/database"
	"gitlab.local/TerraMaster/tos-modules/database/sqlitemodel"
	"gitlab.local/TerraMaster/tos-modules/directory"
	"gitlab.local/TerraMaster/tos-modules/service/disk"
	"gitlab.local/TerraMaster/tos-modules/service/sharefolder"
	"gitlab.local/TerraMaster/tos-modules/servicectl"
	"os"
)

const snapshotName = "Snapshot"

var FileExists = errors.New("file exists")

type SecretKey map[string]SecretKeyInfo

type UserAttrs fileoperate.UserAttrs

type SecretKeyInfo struct {
	Passphrase  string `json:"passphrase"`
	Passtip     string `json:"passtip"`
	EcryptfsSig string `json:"ecryptfs_sig"`
}

type SnapshotConf struct {
	Type        string `json:"type"`
	Volume      string `json:"volume"`
	DistDirName string `json:"dist_dir_name"`
	Lock        bool   `json:"lock"`
}

type Service interface {
	IsExistsFolder(string) bool
	IsExistsFolderCaseInsensitive(string) bool
	SetNFSAcl(arg sharefolder.NFSAcl) error
	GetNFSAclList(folderId int) ([]sharefolder.NFSAcl, error)
	DelNFSAcl(hashId string) error
	UpdateNFSAcl(arg sharefolder.NFSAcl) error
	ScanIsoFiles() (result []string)
	GetFolderList(name string) []sharefolder.ShareFolder
	GetIsoList() []sqlitemodel.Share
	CreateIsoFolder(option sqlitemodel.Share) error
	DeleteIsoFolder(id int) error
	GetFolderInfo(int) (*sharefolder.Detail, error)
	CreateFolder(string, *directory.CreateFolder) error
	GetFolderFromDb(name string) *directory.CreateFolder
	Acl() acl.FolderAttrService
	EditFolderRichAcl(string, acl.FolderAttrs) error
	GetFolderRichAclInfo(string) (*acl.FolderAttrs, error)
	//GetRichAclUserList(path string) acl.RichAclUser
	EditFolder(int, sharefolder.Detail) error
	EditFolderPermission(int, sharefolder.Detail) error
	DeleteFolder(int) error
	ObtainSecretKey(string, string) (*sharefolder.ObtainSecretKeyData, error)
	EcryptfsMount(string, string) (bool, error)
	EcryptfsUmount(string) (bool, error)
	ChangeVolume(newRoot string) error
	GetSmbAclList(folderId int) ([]sharefolder.SmbAcl, error)
	UpdateSmbAcl(arg sharefolder.SmbAcl) error
	DelSmbAcl(hashString string) error
	SetSmbAcl(arg sharefolder.SmbAcl) error
	GetWebdavAclList(folderId int) ([]sharefolder.SmbAcl, error)
	UpdateWebdavAcl(arg sharefolder.SmbAcl) error
	DelWebdavAcl(hashString string) error
	SetWebDavAcl(arg sharefolder.SmbAcl) error
	SnapshotOccupationFolder() []string
	MvShareFolder(path, volume, uname string, ginCtx context.Context) (int, error)
	MvShareFolderPlan() (*sharefolder.MvShareFolderPlan, error)
	ShareFolderListByVolume(string, string) (interface{}, error)
	SetRecycleStatus(path string, b int) error
}

type Folder struct {
	dirService            directory.FolderService
	userService           user.UserService
	aclService            acl.FolderAttrService
	diskService           disk.Getter
	dbService             database.DbGetter
	ctlService            servicectl.Ctl
	shareService          fileoperate.Getter
	mvShareFolderCancelFn context.CancelFunc //移动共享文件夹时取消函数
	logFile               *os.File
	folders               folder.FoldersService
}

func New(service directory.FolderService,
	userService user.UserService,
	aclService acl.FolderAttrService,
	diskService disk.Getter,
	dbService database.DbGetter,
	ctlService servicectl.Ctl,
	shareService fileoperate.Getter,
	folderObj folder.FoldersService) *Folder {
	f := &Folder{
		dirService:   service,
		userService:  userService,
		aclService:   aclService,
		diskService:  diskService,
		dbService:    dbService,
		ctlService:   ctlService,
		shareService: shareService,
		folders:      folderObj,
	}
	//还原加密共享文件夹秘钥
	go f.restoreSecretKey()
	return f
}
